package com.xhtt.modules.app.controller.reportform;

import com.xhtt.common.exception.RRException;
import com.xhtt.common.exception.RRExceptionEnum;
import com.xhtt.common.utils.Constant;
import com.xhtt.common.utils.ExportExcel;
import com.xhtt.common.utils.R;
import com.xhtt.modules.reportform.model.DiscussCompanyTime;
import com.xhtt.modules.reportform.service.VitReportService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 统计分析-VIT统计报表
 *
 * @Date 2019/3/15 14:32
 **/

@RestController
@RequestMapping(value = "app/report/vit")
@Api(value = "统计分析-VIT统计报表", tags = "统计分析-VIT统计报表")
public class VITReportController {

    @Autowired
    private VitReportService vitReportService;

    @GetMapping(value = "/total")
    @ApiOperation("VIT时间总表(web)")
    public R getTotal(@RequestParam Map<String, Object> params) {
        return R.ok().put(Constant.DATA, vitReportService.total(params));
    }

    @GetMapping(value = "/total/export")
    @ApiOperation("VIT时间总表-导出(web)")
    public void totalExport(HttpServletResponse response, @RequestParam Map<String, Object> params) {
        List<Map<String, Object>> result = vitReportService.total(params);
        ExportExcel exportExcel = new ExportExcel();
        params.put("limit", "650000");

        String fileName = "VIT时间总表";
        String sheetName = "日期";
        String[] headers = {"船名", "VIT时间"};
        String[] columns = {"ship_name", "show_time"};

        if (params.get("type").toString().equals("2")) {
            fileName = "VIT时间总表";
            sheetName = "年份";
            headers = new String[]{"月份", "每月VIT时间", "船舶数量"};
            columns = new String[]{"date", "show_time", "count"};
        }

        try {
            exportExcel.exportExcel(fileName, sheetName, headers, columns, result, response);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RRException(RRExceptionEnum.EXPORT_FAIL);
        }
    }

    @GetMapping(value = "/discuss")
    @ApiOperation("商检时间统计(web)")
    public R getDiscuss(@RequestParam Map<String, Object> params) {
        List<Map<String, Object>> result;
        Integer type = Integer.parseInt(params.get("type").toString());
        if (type.equals(1)) {
            result = vitReportService.discussTime(params);
            //处理
            Set<String> shipNames = result.stream().map(m -> (String) m.get("ship_name")).collect(Collectors.toSet());//所有船名
            Set<String> companyNames = result.stream().map(m -> (String) m.get("company_name")).collect(Collectors.toSet());//所有公司名
            //构造一个字典
            Map<String, DiscussCompanyTime> map = new HashMap<>();
            result.forEach(m -> {
                String key = m.get("ship_name") + "$" + m.get("company_name");
                Integer time = null == m.get("time") ? 0 : Integer.valueOf(m.get("time").toString());
                String showTime = null == m.get("show_time") ? "0时0分" : (String) m.get("show_time");
                DiscussCompanyTime dct = new DiscussCompanyTime("", time, showTime);
                map.put(key, dct);
            });

            List<List<DiscussCompanyTime>> parent = new ArrayList<>();

            for (String companyName : companyNames) {
                List<DiscussCompanyTime> child = new ArrayList<>();
                for (String shipName : shipNames) {
                    child.add(map.get(shipName + "$" + companyName));
                }
                parent.add(child);
            }


//            List<DiscussShip> discussShipList = new ArrayList<>();
//            shipNames.forEach(s -> discussShipList.add(new DiscussShip(s)));
//
//            for (DiscussShip ds : discussShipList) {
//                String shipName = ds.getShipName();
//                List<DiscussCompanyTime> discussCompanyTimeList = ds.getCompanyTimeList();
//                for (String companyName : companyNames) {
//                    DiscussCompanyTime dct = map.get(shipName + "$" + companyName);
//                    if (null == dct) {
//                        dct = new DiscussCompanyTime(companyName, 0, "0时0分");
//                    } else {
//                        dct.setCompanyName(companyName);
//                    }
//                    System.out.println("aa");
//                    discussCompanyTimeList.add(dct);
//                }
//            }

            return R.ok().put(Constant.DATA, result).put("shipNames", shipNames).put("companyNames", companyNames).put("parent", parent);
        } else if (type.equals(2)) {
            result = vitReportService.discussProduct(params);
            return R.ok().put(Constant.DATA, result);
        } else {
            return R.error("数据异常");
        }

    }

    @GetMapping(value = "/discuss/export")
    @ApiOperation("商检时间统计-导出(web)")
    public void discussExport (HttpServletResponse response, @RequestParam Map<String, Object> params) {

        List<Map<String, Object>> result = vitReportService.discussTime(params);
        ExportExcel exportExcel = new ExportExcel();
        params.put("limit", "650000");

        String fileName = "商检时间统计";
        String sheetName = "商检时间";
        String[] headers = {"船名", "商检公司", "时间"};
        String[] columns = {"ship_name", "company_name", "show_time"};

        if (params.get("type").toString().equals("2")) {
            result = vitReportService.discussProduct(params);
            fileName = "商检时间统计";
            sheetName = "产品";
            headers = new String[]{"产品", "时间"};
            columns = new String[]{"product_name", "show_time"};
        }

        try {
            exportExcel.exportExcel(fileName, sheetName, headers, columns, result, response);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RRException(RRExceptionEnum.EXPORT_FAIL);
        }

    }

    @GetMapping(value = "/lm")
    @ApiOperation("L/M作业时间统计(web)")
    public R getLm(@RequestParam Map<String, Object> params) {
        return R.ok().put(Constant.DATA, vitReportService.lm(params));
    }

    @GetMapping(value = "/lm/export")
    @ApiOperation("L/M作业时间统计-导出(web)")
    public void lmExport(HttpServletResponse response, @RequestParam Map<String, Object> params) {
        List<Map<String, Object>> result = vitReportService.lm(params);
        ExportExcel exportExcel = new ExportExcel();
        params.put("limit", "650000");

        String fileName = "L/M作业时间统计";
        String sheetName = "日期";
        String[] headers = {"船名", "L/M作业时间"};
        String[] columns = {"ship_name", "show_time"};

        if (params.get("type").toString().equals("2")) {
            fileName = "L/M作业时间统计";
            sheetName = "年份";
            headers = new String[]{"月份", "L/M作业时间"};
            columns = new String[]{"month", "sum"};
        }

        try {
            exportExcel.exportExcel(fileName, sheetName, headers, columns, result, response);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RRException(RRExceptionEnum.EXPORT_FAIL);
        }
    }

    @GetMapping(value = "/berth")
    @ApiOperation("靠泊到启泵时间统计(web)")
    public R getBerth(@RequestParam Map<String, Object> params) {
        return R.ok().put(Constant.DATA, vitReportService.berth(params));
    }

    @GetMapping(value = "/berth/export")
    @ApiOperation("靠泊到启泵时间统计-导出(web)")
    public void berthExport(HttpServletResponse response, @RequestParam Map<String, Object> params) {
        List<Map<String, Object>> result = vitReportService.berth(params);
        ExportExcel exportExcel = new ExportExcel();
        params.put("limit", "650000");

        String fileName = "靠泊到启泵时间统计";
        String sheetName = "日期";
        String[] headers = {"泊位号", "靠泊到启泵时间"};
        String[] columns = {"ship_name", "show_time"};

        if (params.get("type").toString().equals("2")) {
            fileName = "靠泊到启泵时间统计";
            sheetName = "年份";
            headers = new String[]{"月份", "靠泊到启泵时间"};
            columns = new String[]{"month", "sum"};
        }

        try {
            exportExcel.exportExcel(fileName, sheetName, headers, columns, result, response);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RRException(RRExceptionEnum.EXPORT_FAIL);
        }
    }

}
